﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using System.Configuration;
using DataModel.Models;
using System.Linq;

namespace DataModel.Managers.SubManage
{
    public class MailSender
    {
        /// <summary>
        /// Sends login data
        /// </summary>
        /// <param name="login"></param>
        /// <param name="passwod"></param>
        /// <param name="addresseeName"></param>
        /// <param name="addresseeMail"></param>
        /// Exceptions: Errors with letter sending
        public void SendLoginData(string login, string passwod, string addresseeName, string addresseeMail)
        {
            string subject = En.RegistrationLetterSubject;

            string letterBody = En.LoginInfoLetter_ENG;
            letterBody = letterBody.Replace(En.LetterUserNameTag, addresseeName);
            letterBody = letterBody.Replace(En.LetterLoginTag, login);
            letterBody = letterBody.Replace(En.LetterPasswordTag, passwod);
            letterBody = letterBody.Replace(En.LetterUniversityTag, En.UniversityName);
            SendMail(addresseeMail, subject, letterBody);
        }

        /// <summary>
        /// Sends edit schedule data
        /// </summary>
        /// <param name="addresseeName"></param>
        /// <param name="addresseeMail"></param>
        /// <param name="changedGroup"></param>
        /// Exceptions: Errors with letter sending
        public void SendEditScheduleLetter(string addresseeName, string addresseeMail, string changedGroup)
        {
            string subject = En.ScheduleEditLetterSubject;

            string letterBody = En.EditScheduleLetter_ENG;
            letterBody = letterBody.Replace(En.LetterLoginTag, addresseeName);
            letterBody = letterBody.Replace(En.LetterUniversityTag, En.UniversityName);
            letterBody = letterBody.Replace(En.LetterGroupTag, changedGroup);

            SendMail(addresseeMail, subject, letterBody);
        }

        public void SendEditLessonData(LessonRequest lesson)
        {
            try
            {
                var entitieModel = new UniversityPortalEntities();
                var groupManager = new GroupManager();
                Lesson lessonModel = entitieModel.Lessons.FirstOrDefault(l => l.ID == lesson.Lessonid);

                foreach (var group in lessonModel.Groups)
                {
                    List<StudentModel> students = groupManager.GetAllStudentsByGroup(group.ID);
                    if (students != null)
                    {
                        foreach (var studentModel in students)
                        {
                            SendEditScheduleLetter(
                                studentModel.FamilyName + studentModel.FirstName + studentModel.LastName,
                                studentModel.Email,
                                group.number.ToString());
                        }
                    }
                }
            }
            catch
            {
                return;
            }
        }

        /// <summary>
        /// Sends letter to addressee. 
        /// </summary>
        /// <param name="addresseeMail"></param>
        /// <param name="letterSubject"></param>
        /// <param name="letterBody"></param>
        /// Exceptions: Errors with letter sending
        private void SendMail(string addresseeMail, string letterSubject, string letterBody)
        {
            var fromAddress = new MailAddress(En.UniversityMailAddress);
            var toAddress = new MailAddress(addresseeMail);
            string fromPassword = En.UniversityMailPassword;

            var smtp = new SmtpClient
                {
                    Host = ConfigurationManager.AppSettings["LetterSmtpHost"],
                    Port = int.Parse(ConfigurationManager.AppSettings["LetterSmtpPort"]),
                    EnableSsl = bool.Parse(ConfigurationManager.AppSettings["LetterSmtpEnableSsl"]),
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = bool.Parse(ConfigurationManager.AppSettings["LetterSmtpUseDefaultCredentials"]),
                    Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
                };
            var message = new MailMessage(fromAddress, toAddress)
                {
                    Subject = letterSubject,
                    Body = letterBody
                };

            try
            {
                smtp.Send(message);
            }
            catch (Exception)
            {
            }
        }
    }
}
